package subject_set.offer100;

import java.util.HashMap;
import java.util.Map;

/**
 * @author haomin
 * @date 2022/10/25 15:46
 **/
public class Offer57 {
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
            for(int i : nums){
                if(map.containsKey(i)) return new int[]{i, map.get(i)};
                map.put(target - i, i);
            }
            return new int[0];
        }
    }
    class Solution2 {
        public int[] twoSum(int[] nums, int target) {
            int l = 0, r = nums.length-1;
            while(l < r){
                if(nums[l] + nums[r] < target){
                    l++;
                }else if(nums[l] + nums[r] > target){
                    r--;
                }else return new int[]{nums[l], nums[r]};
            }
            return new int[0];
        }
    }
}